package org.csu.xzypetstore.controller;

import org.csu.xzypetstore.domain.Account;
import org.csu.xzypetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/account/")
@SessionAttributes({"account", "msg", "USER", "PWD", "success","categories","languages","message"})
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("signOn")
    public String SignOn(HttpServletRequest req, String username, String password, Model model) {
        String code = req.getParameter("code");
        // 验证验证码
        String sessionCode = (String) req.getSession().getAttribute("imgcode");

        Account account = accountService.getAccount(username, password);
        if (account == null) {
            model.addAttribute("success", 11);
            String msg = "用户名或者密码错误";
            model.addAttribute("msg", msg);
            return "account/SignonForm";
        } else if (code.equalsIgnoreCase(sessionCode)) {
            model.addAttribute("account", account);
            model.addAttribute("USER", username);
            model.addAttribute("PWD", password);
            return "catalog/main";
        } else {
            String yanmag = "验证码输入错误";
            model.addAttribute("USER", username);
            model.addAttribute("PWD", password);
            model.addAttribute("success", 111);
            model.addAttribute("account", account);
            return "account/SignonForm";
        }
    }


    @PostMapping("newAccount")
    public String newAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repeatedPassword = request.getParameter("repeatedPassword");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
//        String firstName = request.getParameter("firstName");
//        String lastName = request.getParameter("lastName");
//        String address1 = request.getParameter("address1");
//        String address2 = request.getParameter("address2");
//        String city = request.getParameter("city");
//        String state = request.getParameter("state");
//        String zip = request.getParameter("zip");
//        String country = request.getParameter("country");
//        String languages = request.getParameter("languagePreference");
//        String categories = request.getParameter("favouriteCategoryId");
//        String email = "email";
//        String phone = "phone";
        String firstName = "firstName";
        String lastName = "lastName";
        String address1 = "address1";
        String address2 = "address2";
        String city = "city";
        String state = "state";
        String zip = "zip";
        String country = "country";
        String languages = "languagePreference";
        String categories = "favouriteCategoryId";


        boolean listOption;
        if (request.getParameter("listOption") != null) {
            listOption = true;
        } else {
            listOption = false;
        }

        boolean bannerOption;
        if (request.getParameter("bannerOption") != null) {
            bannerOption = true;
        } else {
            bannerOption = false;
        }

        if (password.equals(repeatedPassword) && username.length() > 0) {
            Account account = new Account();

            account.setUsername(username);
            account.setPassword(password);
            account.setFirstName(firstName);
            account.setLastName(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddress1(address1);
            account.setAddress2(address2);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            account.setLanguagePreference(languages);
            account.setFavouriteCategoryId(categories);
            account.setListOption(listOption);
            account.setBannerOption(bannerOption);

            accountService.insertAccount(account);

            return "account/SignonForm";
        } else {
            return "account/NewAccountForm";
        }
    }

    @GetMapping("EditnewAccount")
    public String NewAccount() {
        return "account/NewAccountForm";
    }

    @GetMapping("editAccount")
    public String editAccount(Model model)
    {
        List<String> languages = new ArrayList<String>();
        languages.add("Japanese");
        languages.add("English");
        languages.add("Spanish");
        languages.add("Russian");
        languages.add("Chinese");
        model.addAttribute("languages",languages);

        List<String> categories = new ArrayList<>();
        categories.add("FISH");
        categories.add("DOGS");
        categories.add("REPTILES");
        categories.add("CATS");
        categories.add("BIRDS");
        model.addAttribute("categories",categories);

        return "account/editAccount";
    }


    @PostMapping("confirmEdit")
    public String confirmEdit(HttpServletRequest request,Model model) throws ServletException, IOException {


        String password = request.getParameter("password");
        String repeatedPassword = request.getParameter("repeatedPassword");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String address1 = request.getParameter("address1");
        String address2 = request.getParameter("address2");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String country = request.getParameter("country");
        String languages = request.getParameter("languagePreference");
        String categories = request.getParameter("favouriteCategoryId");

        boolean listOption;
        if(request.getParameter("listOption") != null){
            listOption =  true;
        }else{
            listOption = false;
        }

        boolean bannerOption;
        if(request.getParameter("bannerOption") != null){
            bannerOption =  true;
        }else{
            bannerOption = false;
        }

        //判断信息是否修改成功
        String sss;
        if(password.equals(repeatedPassword)){



            Account account;
            HttpSession session = request.getSession();
            account = (Account)session.getAttribute("account");
            account.setPassword(password);
            account.setFirstName(firstName);
            account.setLastName(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddress1(address1);
            account.setAddress2(address2);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            account.setLanguagePreference(languages);
            account.setFavouriteCategoryId(categories);
            account.setListOption(listOption);
            account.setBannerOption(bannerOption);

            accountService.update(account);
            // 覆盖原来的account
            model.addAttribute("account",account);
            sss="success";
            model.addAttribute("sss",sss);
            return "admin/ViewUser";

        }else{
            sss="password error";
            model.addAttribute("sss",sss);
            return "admin/ViewUser";
        }
    }

    @GetMapping("resetPassword")
    public String resetPassword(String username,Model model){


        String password = "123456";

            Account account=new Account();
            account.setPassword(password);
            account.setUsername(username);

            accountService.resetPassword(account);

            account = accountService.getAccountByUsername(username);
            // 覆盖原来的account
            model.addAttribute("account",account);
            String sss="resetSuccess";
            model.addAttribute("sss",sss);
            return "admin/ViewUser";
    }

}
